import json

import flask_restful
from flask import Flask, jsonify, make_response, request
from flask_cors import CORS
import requests
import json
from sum import MyMong, userInsart
from bson.objectid import ObjectId
import time

app = Flask(__name__)
# 用户密码信息类初始化
mymong = MyMong()
# 用户信息初始化
userInsart = userInsart()
# 跨域设置
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

'''
#路由部分
'''
# 测试


@app.route('/api/showPass', methods=['get'])
def get_task():

    userid = request.args.get('userid')
    id = request.args.get('id')
    if userid == 0:
        return "404"
    value = mymong.info(userid=userid, id=id)
    return value

# 展示用户全部密码


@app.route('/api/all', methods=['get'])
def get_all():
    userid = request.args.get('userid')
    value = mymong.allInfo(userid)
    return value

# 用户添加密码


@app.route('/api/add', methods=['get'])
def get_add():
    userid = request.args.get('userid')
    passwd = request.args.get('passwd')
    name = request.args.get('name')

    value = mymong.adds(name=name, passwd=passwd, userId=userid)
    return value

# 添加用户


@app.route('/api/userinsart', methods=['get'])
def get_user_userinsart():
    userid = request.args.get('userid')
    username = request.args.get('username')
    avatarUrl= request.args.get('avatarUrl')
    value = userInsart.userinsart(userid=userid, username=username,avatarUrl=avatarUrl)
    return value


@app.route('/api/getUserId', methods=['get'])
def get_userid():
    code = request.args.get('code')
    url = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxcc9426e7bf4e76bd&secret=4082982d2e1c5dd6ded08755f63a89e2&js_code=' + \
        code+'&grant_type=authorization_code'
    dom = requests.get(url)
    return dom.text


@app.route('/api/getAccessToken', methods=['get'])
def get_accesstoken():
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxcc9426e7bf4e76bd&secret=4082982d2e1c5dd6ded08755f63a89e2'
    dom = requests.get(url)
    return dom.text


@app.route('/api/sendMessage', methods=['post'])
def send_url():
    urls = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxcc9426e7bf4e76bd&secret=4082982d2e1c5dd6ded08755f63a89e2'
    doms = requests.get(urls)
    tokens =  doms.text
    url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='+str(json.loads(tokens)['access_token'])

    touser = request.form['touser']
    template_id = request.form['template_id']
    form_id= request.form['form_id']
    key1 = request.form['key1']
    key2 = request.form['key2']
    key3 = request.form['key3']
    data =json.dumps({
        'touser': touser,
        'template_id': template_id,
        'form_id': form_id,
        'data':  {   "keyword1": {
                                        "value": key1
                                    },
                                    "keyword2": {
                                        "value": key2
                                    },
                                    "keyword3":{
                                        "value":key3
                                    }
                                    }
    })    
    dom = requests.post(url,data=data)
    return dom.text
    # return str(data)




if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8081, debug=True)
